//
//  CollectionHeaderView.m
//  Stream
//
//  Created by shaw on 16/8/14.
//  Copyright © 2016年 showshaw. All rights reserved.
//

#import "CollectionHeaderView.h"
#import "Masonry.h"
#import "SlideModel.h"
#import "UIImageView+WebCache.h"
#import "XXNetRequest.h"
#import "MJExtension.h"
#import "Global.h"

@interface CollectionHeaderView()<UIScrollViewDelegate>
/**
 *  Scroll View
 */
@property (nonatomic,strong) UIScrollView* scrollView;
/**
 *  页码控制
 */
@property (nonatomic,strong) UIPageControl* pageControl;
/**
 *  轮播定时器
 */
@property (nonatomic, strong) NSTimer *timer;
/**
 *  timeLabel
 */
@property (nonatomic,strong) UILabel* timeLabel;
/**
 *  dateLabel
 */
@property (nonatomic,strong) UILabel* cityLabel;
/**
 *  tempLabel
 */
@property (nonatomic,strong) UILabel* tempLabel;
@end

@implementation CollectionHeaderView


-(void)layoutSubviews
{
    //jiao hu
    self.userInteractionEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    
    //加载轮播图片
    [self loadScrollView];
    
    //加载定时器
    [self loadTimer];
}

//点击轮播图
-(void)tappedSlide:(UIGestureRecognizer*)gesture
{
    UIView *currentView = gesture.view;
    if([self.delegate respondsToSelector:@selector(collectionViewDidClickSlide:)])
    {
        [self.delegate collectionViewDidClickSlide:currentView];
    }
}
//加载轮播图片
- (void)loadScrollView{
    
    UIView *imageView = [self firstImageView];
    [self.scrollView addSubview:imageView];
    
    for (int i = 0; i < _dataArray.count; i++) {
        SlideModel * model = _dataArray[i];
        
        //计算imageView的X的坐标
        CGFloat imageViewX = (i + 1) * Screen_W_;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageViewX, 0, Screen_W_, 160.0f)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i + 100;

        [imageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"weather_sunny_bg"]];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedSlide:)];
        [imageView addGestureRecognizer:tap];
        
        UIView *divider = [[UIView alloc] init];
        divider.backgroundColor = [UIColor blackColor];
        divider.alpha = 0.5;
        [imageView addSubview:divider];
        [divider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@30);
            make.bottom.left.right.equalTo(imageView);
        }];
        [imageView bringSubviewToFront:divider];
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:13];
        label.text = model.name;
        [divider addSubview:label];
        [divider bringSubviewToFront:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(divider);
            make.left.equalTo(divider.mas_left).offset(8);
        }];
        
        //添加到轮播控制器中
        [self.scrollView addSubview:imageView];
    }
    //计算imageView的宽度
    CGFloat imageViewW = (_dataArray.count + 1) * Screen_W_;
    
    //给轮播器设置滚动范围
    self.scrollView.contentSize = CGSizeMake(imageViewW, 160.0f);
    //隐藏滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //设置分页
    self.scrollView.pagingEnabled = YES;
    //设置代理
    self.scrollView.delegate = self;
}

-(void)setWeather:(WeatherModel *)weather
{
    _weather = weather;
    
    if (weather != nil) {
        _timeLabel.text = self.weather.date;
        _cityLabel.text = [NSString stringWithFormat:@"%@  %@°",self.weather.city,self.weather.temp];
        _tempLabel.text = [NSString stringWithFormat:@"%@  %@° - %@°",self.weather.weather,self.weather.l_tmp,self.weather.h_tmp];
    }
}

-(UIView*)firstImageView
{

    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W_, 160.0f)];
    firstView.userInteractionEnabled = YES;
    firstView.tag = 999;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    //设置图片
    imageView.image = [UIImage imageNamed:@"weather_sunny_bg"];
    
    [firstView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(firstView);
    }];
    
    //UIlabel
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.textColor = [UIColor whiteColor];
    _timeLabel.font = [UIFont systemFontOfSize:15];
    _timeLabel.text = self.weather.date;
    
    _cityLabel = [[UILabel alloc] init];
    _cityLabel.textColor = [UIColor whiteColor];
    _cityLabel.text = [NSString stringWithFormat:@"%@  %@°",self.weather.city,self.weather.temp];
    _cityLabel.font = [UIFont systemFontOfSize:30];
    
    _tempLabel = [[UILabel alloc] init];
    _tempLabel.textColor = [UIColor whiteColor];
    _tempLabel.font = [UIFont systemFontOfSize:15];

    _tempLabel.text = [NSString stringWithFormat:@"%@  %@° - %@°",self.weather.weather,self.weather.l_tmp,self.weather.h_tmp];
    
    [firstView addSubview:_timeLabel];
    [firstView addSubview:_cityLabel];
    [firstView addSubview:_tempLabel];
    [firstView bringSubviewToFront:_timeLabel];
    [firstView bringSubviewToFront:_cityLabel];
    [firstView bringSubviewToFront:_tempLabel];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(firstView.mas_top).offset(10);
        make.left.equalTo(firstView.mas_left).offset(10);
    }];
    
    [_cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(firstView.mas_centerY).offset(-10);
        make.left.equalTo(_timeLabel.mas_left);
    }];
    
    [_tempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(firstView.mas_right).offset(-30);
        make.bottom.equalTo(firstView.mas_bottom).offset(-20);
    }];
    
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedSlide:)];
    [firstView addGestureRecognizer:tap];
    
    return firstView;
}

//加载定时器
- (void)loadTimer{
    [self.timer invalidate];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(pageChanged:) userInfo:nil repeats:YES];
}

//当页码发生改变的时候调用
- (void)pageChanged:(id)sender{
    //获取当前页面的索引
    NSInteger currentPage=self.pageControl.currentPage;
    //获取偏移量
    CGPoint offset=self.scrollView.contentOffset;

    if (currentPage >= _dataArray.count) {
        //将其设置首张图片的索引
        currentPage = 0;
        //恢复偏移量
        offset.x = 0;
        
    }else{
        //当前索引+1
        currentPage ++;
        //设置偏移量
        
        offset.x += Screen_W_;
    }
    //设置当前页
    self.pageControl.currentPage = currentPage;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = offset;
    }];

}

#pragma mark - ScrollView Delegate
// scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取偏移量
    CGPoint offset=scrollView.contentOffset;
    //计算当前页码
    NSInteger currentPage=offset.x / Screen_W_;
    //设置当前页码
    self.pageControl.currentPage=currentPage;
}
//设置代理方法,当开始拖拽的时候,让计时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //使定时器失效
    [self.timer invalidate];
}

//设置代理方法,当拖拽结束的时候,调用计时器,让其继续自动滚动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //重新启动定时器
    [self loadTimer];
}

#pragma mark - Lazy iniliation
-(UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);//.offset(10);
            make.right.equalTo(self.mas_right);//.offset(-10);
            make.height.equalTo(@(160));
            make.top.equalTo(self.mas_top).offset(10);
            
        }];
    }
    return _scrollView;
}

-(UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] init];
//        _pageControl.center = self.scrollView.center;
        //设置当前页码
        _pageControl.currentPage = 0;
        //设置当前页码的颜色
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        //设置其他页码的颜色
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        //加载页码
        self.pageControl.numberOfPages = _dataArray.count + 1;
        
        [self addSubview:_pageControl];
        [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.scrollView);
            make.bottom.equalTo(self.scrollView.mas_bottom);
        }];
        
        [self bringSubviewToFront:_pageControl];
    }
    return _pageControl;
}
@end
